package com.iwhalecloud.uncc.service.impl;

import com.iwhalecloud.uncc.common.ArchiverConstant;
import com.iwhalecloud.uncc.common.ArchiverThreadLocal;
import com.iwhalecloud.uncc.common.DataSourceHelp;
import com.iwhalecloud.uncc.common.SysConstant;
import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.domain.InsCollectServerArchiverMsg;
import com.iwhalecloud.uncc.exception.ArchiverException;
import com.iwhalecloud.uncc.model.InsCollectServer;
import com.iwhalecloud.uncc.model.InsCollectServerResRate;
import com.iwhalecloud.uncc.properties.mq.ArchiverTopicProperties;
import com.iwhalecloud.uncc.service.ArchiverService;
import com.iwhalecloud.uncc.utils.ArchiverUtil;
import com.iwhalecloud.uncc.utils.CheckUtil;
import com.iwhalecloud.uncc.utils.DataSourceUtil;
import com.iwhalecloud.uncc.utils.DivideUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/26 17:26
 * @description
 */
@Service("insCollectServerArchiverService")
@Slf4j
public class InsCollectServerArchiverServiceImpl implements ArchiverService<InsCollectServer> {

    @Autowired
    private ArchiverDao insCollectServerArchiverDao;

    @Autowired
    private ArchiverDao insCollectServerResRateArchiverDao;

    @Autowired
    private ArchiverTopicProperties archiverTopicProperties;

    @Autowired
    private DataSourceUtil dataSourceUtil;
    @Autowired
    private DataSourceHelp dataSourceHelp;

    @Async("taskExecutor")
    @Override
    public void archive(String partitionCode) {
        DatasourceContextHolder.setDs(partitionCode);
        this.doArchive();
        DatasourceContextHolder.setDs(SysConstant.DB_DEFAULT_PARTITION);
    }


    private void doArchive()  {
        long s = System.currentTimeMillis();
        if (log.isInfoEnabled()) {
            log.info(Thread.currentThread().getName() + "执行归档  "
                   + DatasourceContextHolder.getDs() + "数据库 InsCollectServer 关联数据任务 ...");
        }

        //查所有的InsCollectServer
        List<InsCollectServer> list = insCollectServerArchiverDao.queryList(null);

        if(list == null){
            if (log.isInfoEnabled()) {
                log.info("此次归档任务insCollectServerList=NULL");
                log.info(Thread.currentThread().getName() + "完成任务:NULL耗时:"
                        + ((System.currentTimeMillis() - s) / 1000) + "s");
            }
            return;
        }

        DivideUtil.divideSend(this,list);


        if (log.isInfoEnabled()) {
            log.info(Thread.currentThread().getName() + "完成归档发送任务  耗时:"
                    + ((System.currentTimeMillis() - s) / 1000) + "s");
        }
    }

    @Override
    public void packMsg(List<InsCollectServer> list) {

        List<Integer> ids = new ArrayList<>();
        list.forEach(i->ids.add(i.getId()));

        List<InsCollectServerResRate> insCollectServerResRateList = CheckUtil.batchQuery(insCollectServerResRateArchiverDao,ids);

        if(insCollectServerResRateList == null) {
            return;
        }

        //修改Flag
        CheckUtil.batchUpdateArchiving(insCollectServerResRateArchiverDao,insCollectServerResRateList);

        //组装
        InsCollectServerArchiverMsg insCollectServerArchiverMsg = new InsCollectServerArchiverMsg();
        insCollectServerArchiverMsg.setIsDeleteOnline(ArchiverThreadLocal.getArchiveParam().getIsDeleteOnline());
        insCollectServerArchiverMsg.setPartitionCode(DatasourceContextHolder.getDs());
        insCollectServerArchiverMsg.setInsCollectServerList(list);
        insCollectServerArchiverMsg.setInsCollectServerResRateList(insCollectServerResRateList);

        //发送MQ
        try{
            sendMsg(insCollectServerArchiverMsg);
        }catch (ArchiverException a){
            if (log.isInfoEnabled()) {
                log.info(Thread.currentThread().getName() + a.getErrorMessage());
            }
            //重置 Flag
            CheckUtil.batchResetOnline(insCollectServerResRateArchiverDao,insCollectServerResRateList);

            a.printStackTrace();
        }
    }

    private void sendMsg(InsCollectServerArchiverMsg msg) throws ArchiverException {
        String key = ArchiverConstant.PREFIX_INS_COLLECT_SERVER + msg.getInsCollectServerList().get(0).getId();
        String topic = archiverTopicProperties.getInsCollectServerArchiverTopic();
        ArchiverUtil.sendMsg(topic,key,msg);
    }
}
